<?php
namespace App\Http\Controllers\Backstage;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Validator\UserVali;
use App\Model\Backstage\User;
use App\Tool\Error;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Validator;

/**
 * Class UserController
 * @package App\Http\Controllers\Backstage
 * 用户控制器
 * date:2018-4-30
 * author:lxy
 */
class UserController extends Controller
{
    protected $user;

    public function __construct()
    {
       $this->user = new User();
    }

    public function user()
    {
        $user = Auth::user();
        dd($user);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 获取中介公司员工列表
     */
    public function getUserList (Request $request)
    {
        $map['user.is_del'] = 0;
        $map['u_b_id'] = Auth::user()->u_b_id;//公司ID
        //部门ID
        $u_d_id = $request->input('u_d_id');
        if ($u_d_id){
            $map[] = ['u_d_id', '=', $u_d_id];
        }
        //员工名字
        $u_username = $request->input('u_username');
        if ($u_username){
            $map[] = ['u_username', 'like', "%".$u_username."%"];
        }
        //员工账号
        $u_account_number = $request->input('u_account_number');
        if ($u_account_number){
            $map[] = ['u_account_number', 'like', "%".$u_account_number."%"];
        }
        $list = $this->user->getUserList($map, env('PAGE_LIMIT_ADMIN'));
        if(!$list){
            return $this->error(Error::NULL_DATA);
        }
        return $this->success('ok',$list);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 添加中介公司员工账号信息
     */
    public function addUser (Request $request)
    {
        $params = $request->except('token');
        $vali = UserVali::validate($params);
        if(!$vali){
            return $this->error(Error::PARAM_MISSING);
        }
        $params['u_b_id'] = Auth::user()->u_b_id;
        $res = $this->user->addUser($params);
        if($res){
            return $this->success('ok','添加成功！');
        }else{
            return $this->error(Error::ADD_ERROR);
        }
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 修改中介公司员工信息
     */
    public function saveUser (Request $request)
    {
        $params = $request->except(['token','u_account_number']);
        $vali = UserVali::saveValidate($params);
        if(!$vali){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = $this->user->saveUser($params);
        if($res){
            return $this->success('ok','修改成功！');
        }else{
            return $this->error(Error::EDIT_ERROR);
        }
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 删除中介公司员工信息
     */
    public function delUser(Request $request)
    {
        $uid = $request->input('u_id');
        if(!$uid){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = $this->user->delUser($uid);
        if($res){
            return $this->success('ok','删除成功！');
        }else{
            return $this->error(Error::DEL_ERROR);
        }

    }

    /**上级重置员工密码为初始密码
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function resetPassword(Request $request)
    {
        $uid = $request->input('u_id');
        if(!$uid){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = User::resetPassword($uid);
        if (!$res) {
            return $this->error(Error::RESET_PASSWORD);
        }
        return  $this->success('ok', '重置密码成功新密码为（123456）');
    }

    /**修改密码
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function updatePassword(Request $request)
    {
        $params = $request->all();
        $vali = UserVali::updatePassword($params);
        if(!$vali){
            return $this->error(Error::PARAM_MISSING);
        }
        if ($params['new_password'] !== $params['again_password']) {
            return $this->error(Error::TWO_PASSWORD_ERROR);
        }
        $res = User::updatePassword($params['new_password']);
        if (!$res) {
            return $this->error(Error::UPDATE_PASSWORD_FAIL);
        }
        return $this->success('ok', '修改成功！');

    }

}